<?php

namespace app\common\controller;

use think\Controller as tpController;
use think\Session;
use think\Db;

//use think\Env;

class Admin extends tpController {

    public $request;
    public $noLogin = [
        'index/login',
        'index/logout'
    ];
    public $adminUser;

    public function __construct(\think\Request $request = null) {
        parent::__construct($request);
        $this->request = $request;

        $this->setConfigure();

        $this->checkLogin();
    }

    /**
     * 系统配置
     */
    private function setConfigure() {
        $adminConfigure = Session::get('adminConfigure');
        if (empty($adminConfigure)) {
            $adminConfigure = getAdminConfigure();
            Session::set('adminConfigure', $adminConfigure);
        }
        $this->assign('adminConfigure', $adminConfigure);
    }

    private function checkLogin() {
        $module = strtolower($this->request->module());
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());

        $url = "{$controller}/{$action}";
        if (in_array($url, $this->noLogin)) {
            return true;
        }

        $admin = Session::get('admin');
        if (empty($admin)) {
            $this->redirect('index/login', ['url' => base64_encode($url)]);
            exit;
        }

        $where = [
            'm' => $module,
            'c' => $controller,
            'a' => $action,
            'is_del' => 0
        ];

        $menuModel = new \app\admin\model\AdminMenu();
        $curMenu = $menuModel->where($where)->find();

        if (empty($curMenu)) {
            $this->error('未注册的方法');
        }

        $this->assign('curMenu', $curMenu);
        $this->assign('leftmenu', session('leftmenu'));
        $this->assign('admin', $admin);



        $this->adminUser = $admin;
    }

    /**
     * 更新排序
     * @param type $listorder
     * @param type $table
     * @param type $idKey
     * @param type $orderKey
     */
    public function _setorder($listorder, $table, $idKey = 'id', $orderKey = 'list_order') {
        $model = Db::name($table);
        foreach ($listorder as $key => $value) {
            $model->where($idKey, $key)->update([$orderKey => $value]);
        }
    }

    /**
     * 处理单文件上传
     * @param type $inputName
     * @param type $validate
     * @return boolean
     */
    public function _upload($inputName, $validate = []) {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file($inputName);

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate($validate)->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                //echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                return DS . 'uploads' . DS . $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                //echo $info->getFilename();
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        } else {
            return false;
        }
    }

}
